package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.storage.TccTransportationType;
import com.ys.entity.storage.TccPreAllocatePublishItem;
import com.ys.entity.storage.TccTransportationTypeItem;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.storage.StorageMapper;
import com.ys.mapper.storage.TccTransportationTypeItemMapper;
import com.ys.mapper.storage.TccTransportationTypeMapper;
import com.ys.service.storage.TccTransportationTypeService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-05
 * @Description: 运输类型维护
 * @Version: 1.5
 */
@Service("tccTransportationTypeService")
public class TccTransportationTypeServiceImpl implements TccTransportationTypeService {
    private static final Logger logger = LoggerFactory.getLogger(TccTransportationTypeServiceImpl.class);

    @Autowired
    private TccTransportationTypeMapper tccTransportationTypeMapper;

    @Autowired
    private TccTransportationTypeItemMapper tccTransportationTypeItemMapper;

    @Autowired
    private StorageMapper storageMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params,String subData) throws Exception {

        TccTransportationType entity = JSON.parseObject(JSON.toJSONString(params), TccTransportationType.class);

        Integer count = new TccTransportationType().setTransportationTypeCn(entity.getTransportationTypeCn()).setTransportationTypeEn(entity.getTransportationTypeEn()).count();
        if (count != 0){
            return R.error("运输类型已存在,不允许保存!");
        }

        tccTransportationTypeMapper.insert(entity);

        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }
        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            if (detailJO.get("transportationTypeZbCn") == null){
                throw new RuntimeException("运输总称为空,不允许保存!");
            }
            TccTransportationTypeItem tccTransportationTypeItem = JSON.parseObject(JSON.toJSONString(detailJO),TccTransportationTypeItem.class);

            count = new TccTransportationTypeItem().setTransportationTypeZbCn(tccTransportationTypeItem.getTransportationTypeZbCn()).count();
            if (count != 0){
                throw new RuntimeException("运输总称已存在,不允许保存!");
            }
            tccTransportationTypeItem.setStatus(0);
            tccTransportationTypeItem.setTransportationTypeId(entity.getId());
            tccTransportationTypeItemMapper.insert(tccTransportationTypeItem);
        }
        return R.ok(AddDto.closeAndReload());
    }


    @Override
    @Transactional
    public R edit(Map<String, Object> params,String subData) throws Exception {
        TccTransportationType entity = JSON.parseObject(JSON.toJSONString(params), TccTransportationType.class);
        Integer count = new TccTransportationType().setTransportationTypeCn(entity.getTransportationTypeCn()).setTransportationTypeEn(entity.getTransportationTypeEn())
                .where("A.id != " + entity.getId()).count();
        if (count != 0){
            return R.error("运输类型已存在,不允许保存!");
        }
        tccTransportationTypeMapper.update(entity);

        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }

        storageMapper.deleteTransportationTypeItemById(entity.getId());

        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            if (detailJO.get("transportationTypeZbCn") == null){
                throw new RuntimeException("运输总称为空,不允许保存!");
            }
            TccTransportationTypeItem tccTransportationTypeItem = JSON.parseObject(JSON.toJSONString(detailJO),TccTransportationTypeItem.class);

            count = new TccTransportationTypeItem().setTransportationTypeZbCn(tccTransportationTypeItem.getTransportationTypeZbCn())
                    .count();
            if (count != 0){
                throw new RuntimeException("运输总称已存在,不允许保存!");
            }
            tccTransportationTypeItem.setStatus(0);
            tccTransportationTypeItem.setTransportationTypeId(entity.getId());
            tccTransportationTypeItemMapper.insert(tccTransportationTypeItem);
        }
        return R.ok(EditDto.closeAndReload());
    }


}
